package com.crm.model.dto;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.memberManager.CustomerMemberCard;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.permissionManagement.SysCompany;
import com.crm.model.vo.memberManager.MemberShipCardImportVO;
import com.crm.service.memberManager.MemberCardDbService;
import com.crm.service.sys.permissionManagement.SysCompanyDbService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

/**
 * @author luojie
 * @date 2021/6/7 15:07
 */
@Slf4j
public class InsertMemberShipCardCallable implements Callable<JSONObject> {

    private List<MemberShipCardImportVO> list;

    private CountDownLatch begin;

    private CountDownLatch end;

    private SysCompanyDbService sysCompanyDbService;

    private MemberCardDbService memberCardDbService;

    public InsertMemberShipCardCallable(List<MemberShipCardImportVO> list, CountDownLatch begin, CountDownLatch end, SysCompanyDbService companyDbService,MemberCardDbService memberCardDbService){
        this.list = list;
        this.begin = begin;
        this.end = end;
        this.sysCompanyDbService = companyDbService;
        this.memberCardDbService = memberCardDbService;
    }


    @Override
    public JSONObject call() throws Exception {
        //获取登录人信息
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();

        //导入数量
        int importNum = 0;
        //会员卡卡号为空
        List<Integer> membershipCardEmptyIdList = new ArrayList<>();
        //会员卡卡号重复的行数
        List<Integer> repeatIdList = new ArrayList();
/*        //部门名称为空
        List<Integer> companyNameIsNullIdList = new ArrayList<>();
        //所属部门不在
        List<Integer> notExistCompanyIdList = new ArrayList();*/
        //导入失败IdList
        List<Integer> importFailIdList = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            try {

                String membershipCardNumber = list.get(i).getMemberCardNumber();
                if(StringUtils.isBlank(membershipCardNumber)){
                    membershipCardEmptyIdList.add(i+2);
                    continue;
                }else {
                    //查询卡号是否重复
                    CustomerMemberCard customerMemberCard = memberCardDbService.selectMemberShipCardIsRepeat(membershipCardNumber);
                    if(customerMemberCard!=null){
                        repeatIdList.add(i+2);
                        continue;
                    }

                    CustomerMemberCard customerMemberCardAdd = addMemberShipCardModel(list.get(i));
                    Boolean insertResult = memberCardDbService.insertMemberCard(customerMemberCardAdd);
                    if (insertResult) {
                        //导入成功
                        importNum = importNum + 1;
                    } else {
                        //导入失败
                        importFailIdList.add(i+2);
                    }

                }
            } catch (Exception e) {
                //导入失败
                importFailIdList.add(i+2);
                log.error("MemberShipCardImportServiceImpl memberShipCardImport error:", e);
                e.printStackTrace();
            }
            end.countDown();
        }

        JSONObject result = new JSONObject();
        result.put("importNum",importNum);
        result.put("repeatIdList",repeatIdList);
        result.put("membershipCardEmptyIdList",membershipCardEmptyIdList);
        result.put("importFailIdList",importFailIdList);
        return result;
    }

    public CustomerMemberCard addMemberShipCardModel(MemberShipCardImportVO memberShipCardImportVO){
        //当前登录人
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        CustomerMemberCard customerMemberCard = new CustomerMemberCard();
        customerMemberCard.setMemberCardId(CommonUtils.getGUID());
        customerMemberCard.setMemberCardNumber(memberShipCardImportVO.getMemberCardNumber());
        customerMemberCard.setMemberCardPassword(memberShipCardImportVO.getMemberCardPassword());

        List<SysCompany> sysCompanies = sysCompanyDbService.selectSysCompanyList(memberShipCardImportVO.getCompanyName());
        if(sysCompanies.size()>0){
            customerMemberCard.setCompanyId(sysCompanies.get(0).getCompanyId());
        }
        if("电子卡".equals(memberShipCardImportVO.getMemberCardType())){
            customerMemberCard.setMemberCardType("1");
        }else if("实体卡".equals(memberShipCardImportVO.getMemberCardType())){
            customerMemberCard.setMemberCardType("2");
        }
        //卡片状态：0-未发放；1-已发放
        customerMemberCard.setMemberCardStatus("0");
        customerMemberCard.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        //有效日期不导入
/*        customerMemberCard.setStartTm(memberShipCardImportVO.getStartTm());
        customerMemberCard.setEndTm(memberShipCardImportVO.getEndTm());*/
        customerMemberCard.setCreatePeopleId(userAccount.getAccountId());
        customerMemberCard.setCreateTm(new Date());
        return customerMemberCard;
    }
}
